package com.bocaizp.operation.service.impl;

import com.bocaizp.common.constant.MessageConstant;
import com.bocaizp.common.constant.PasswordConstant;
import com.bocaizp.common.constant.StatusConstant;
import com.bocaizp.common.context.BaseContext;
import com.bocaizp.common.exception.AccountLockedException;
import com.bocaizp.common.exception.AccountNotFoundException;
import com.bocaizp.common.exception.PasswordErrorException;
import com.bocaizp.common.result.Result;
import com.bocaizp.operation.mapper.LoginMapper;
import com.bocaizp.operation.service.LoginService;
import com.bocaizp.pojo.VO.CityFlagVO;
import com.bocaizp.pojo.VO.IndustryDirectionVO;
import com.bocaizp.pojo.entity.Employee;
import com.bocaizp.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Author : huang jie
 * Date : 2024/5/2 18:20
 **/
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginMapper loginMapper;

	/**
	 * 用户登录
	 * @param cardId
	 * @param password
	 * @return
	 */
	public User login(String cardId, String password) {
		User user = loginMapper.login(cardId);
		if(user == null){
			throw new AccountLockedException(MessageConstant.ACCOUNT_NOT_FOUND);
		}
		if(!password.equals(user.getPassword())){
			throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
		}
		if(user.getIsMormal() == StatusConstant.ENABLE) {
			throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
		}

		LocalDateTime localDateTime = LocalDateTime.now();
		loginMapper.updateTime(localDateTime,user.getId());
		return loginMapper.getInfo(user.getId());
	}

	/**
	 * 用户注册
	 * @param user
	 */
	public void insert(User user) {
		User user1 = loginMapper.query(user.getCardId());
		if(user1 != null){
			throw new AccountLockedException(MessageConstant.ALREADY_EXISTS);
		}
		loginMapper.insert(user.getCardId(), user.getPassword());
	}

	/**
	 * 找回密码
	 * @param username
	 * @param cardId
	 * @return
	 */
	public String reback(String username, String cardId) {
		User user = loginMapper.queryName(cardId);
		if(user == null){
			throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
		}
		if(!username.equals(user.getUsername())){
			throw new AccountNotFoundException(MessageConstant.USERNAME_ERROR);
		}
		return user.getPassword();
	}

	/**
	 * 获取城市列表
	 *
	 * @return
	 */
	public List<CityFlagVO> getCity() {
		List<CityFlagVO> cityFlagVOList = loginMapper.getFlag();
		for (CityFlagVO cityFlagVO : cityFlagVOList){
			List<String> cityList = loginMapper.getName(cityFlagVO.getFlag());
			cityFlagVO.setCityList(cityList);
		}
		return cityFlagVOList;
	}

	/**
	 * 获取职位分类
	 * @param industry
	 * @return
	 */
	public List<IndustryDirectionVO> getDirection(String industry) {
		List<IndustryDirectionVO> industryDirectionVOS = loginMapper.getDirection(industry);
		for (IndustryDirectionVO in : industryDirectionVOS){
			List<String> joblist = loginMapper.getJob(in.getDirection());
			in.setJobList(joblist);
		}
		return industryDirectionVOS;
	}

	/**
	 * 设置用户意向信息
	 * @param user
	 */
	public void update(User user) {
		Long id = BaseContext.getCurrentId();
		user.setId(id);
		loginMapper.update(user);
	}

	/**
	 * 获取用户基本信息
	 * @return
	 */
	public User getInfo() {
		Long id = BaseContext.getCurrentId();
		return loginMapper.getInfo(id);
	}

	/**
	 * 员工登录
	 * @param empId
	 * @param password
	 * @return
	 */
	public Employee loginEmployee(String empId, String password) {
		Employee employee = loginMapper.getEmployee(empId);
		if(employee == null){
			throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
		}else if(!employee.getPassword().equals(password)){
			throw new AccountNotFoundException((MessageConstant.PASSWORD_ERROR));
		}else if(employee.getIsMormal() == StatusConstant.DISABLE){
			throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
		}
		return employee;
	}

}
